package com.wf.atm.client;


import com.wf.atm.pojo.Account;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;

/**
 * @author Brainrain
 */
public class LoginPanel extends JPanel {

    private ATMClient atmClient;
    public LoginPanel(ATMClient atmClient) {
        this.atmClient = atmClient;
        initComponents();
    }

    //确认按钮
    private void okButtonActionPerformed(ActionEvent e) {

        Account act=null;
        try {
             atmClient.printWriter.println("login"+","+textField.getText()+","+passwordField.getText());
             atmClient.printWriter.flush();
            Object object = atmClient.ois.readObject();
            if(object instanceof  Account)
             {
                 act=  (Account) object;
             }
            else
            {
                JOptionPane.showMessageDialog(null,object.toString());
            }

            atmClient.businessPanel.initBusiness(act);
        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();
            //显示弹窗
            JOptionPane.showMessageDialog(null,ex.getMessage());
        }

        if(act!=null) {
        //直接跳转至BusinessPanel界面
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"businessPanel");
        }

        //清空登录界面数据
        clearLogin();
    }

    //点击取消按钮，回到主界面
    private void cancelButtonActionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"mainPanel");

        //清空登录界面
        clearLogin();
    }

    //清空登陆界面
    public void clearLogin(){
        textField.setText("");
        passwordField.setText("");
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        title = new JLabel();
        idLabel = new JLabel();
        pwdLabel = new JLabel();
        textField = new JTextField();
        passwordField = new JPasswordField();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setLayout(null);

        //---- title ----
        title.setText("登 陆 界 面");
        title.setFont(new Font("Microsoft YaHei", Font.PLAIN, 24));
        title.setHorizontalAlignment(SwingConstants.CENTER);
        add(title);
        title.setBounds(115, 70, 255, 40);

        //---- idLabel ----
        idLabel.setText("用户编号");
        idLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 20));
        idLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(idLabel);
        idLabel.setBounds(55, 140, 125, 32);

        //---- pwdLabel ----
        pwdLabel.setText("用户密码");
        pwdLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 20));
        pwdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(pwdLabel);
        pwdLabel.setBounds(55, 195, 125, 32);
        add(textField);
        textField.setBounds(190, 145, 200, 30);
        add(passwordField);
        passwordField.setBounds(190, 200, 200, 30);

        //---- okButton ----
        okButton.setText("确认");
        okButton.addActionListener(e -> okButtonActionPerformed(e));
        add(okButton);
        okButton.setBounds(115, 285, 92, 30);

        //---- cancelButton ----
        cancelButton.setText("取消");
        cancelButton.addActionListener(e -> cancelButtonActionPerformed(e));
        add(cancelButton);
        cancelButton.setBounds(270, 285, 92, 30);

        setPreferredSize(new Dimension(500, 360));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel title;
    private JLabel idLabel;
    private JLabel pwdLabel;
    private JTextField textField;
    private JPasswordField passwordField;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
